$(function() {
    /**
     * 获取最大出院日期
     */
    $.post(contextPath + "/DrgFeeMedins/initeDateTimePicker.dhc").done(function(result) {
        if (result.success && result.data && result.data.startDateMonth) {
            var date = result.data;
            $("#"+tabId+" #endDate").val(date.lastDate);
            $("#"+tabId+" #startDate").val(date.startDateMonth);
            $("#"+tabId+" #startDate").datetimepicker({
                format : "yyyy-mm-dd",
                autoclose : true,
                todayBtn : true,
                minView : 2,
                language : 'zh-CN'
            });
            $("#"+tabId+" #endDate").datetimepicker({
                format : "yyyy-mm-dd",
                autoclose : true,
                todayBtn : true,
                minView : 2,
                language : 'zh-CN',
            });
            getDepMedinsQuota();
        } else {
            drgUI.warning('出院日期为空、院区编码为空！');
        }
    });
	// 查询
	$("#"+tabId+ " #search_png").click(function(){
		getDepMedinsQuota();
	})
})
//获取科室医保付费统计分析数据
function getDepMedinsQuota(){
		var role = $('#roleName_id').val();
		var deps = $('#deps_id').val();
		var param=drgUI.setAjaxData(tabId);
		param.dateFrom=$("#"+tabId+" #startDate").val();
        param.dateTo= $("#"+tabId+" #endDate").val();
		if(role=="科室"){
			param.pardepCode=deps;
		}
		$.post(contextPath + '/DrgFeeMedins/getDepMedinsQuota.dhc',param).done(function(result) {
			var data=[];
			if(result.success&& result.data&&result.data.length>0){
				data=result.data;
			}
				var columns = [ {
					"data" : "OUT_DEPDESC",
					"title":"出院科室"
				}, {
					"data" : "TOTALCASECNT",
					"title":"出院病例数",
            	    "render":function ( tdData, type, rowData, meta ) {
            		   //tdData:当前cell的值（基于 columns.data）      rowData:整个row的数据（不基于 columns.data）
            		   //meta.row 行下标从0开始，meta.col 列下标从0开始
            		   //如果是过滤或者排序则返回原始数据
				    	if(type =='filter'||type =='sort'){
				    		return tdData;
				    	}
				    	return tdData > 0 ? "<a href='javascript:drgFeeMedinsOutCaseData(\""+ rowData.OUT_DEPCODE + "\",\""+ rowData.OUT_DEPDESC + "\",\""+ param.dateFrom + "\",\""+ param.dateTo + "\")'>"
				    			+ formatNumber(tdData,0)+ "</a>" : '0';
            	    }
				}, {
					"data" : "AVGDAYS",
					"title":"平均住院日"
				}, {
					"data" : "AVGFEE",
					"title":"次均费用(元)",
					"render":function(obj, type, full){
						return formatNumber(full.AVGFEE);
					}
				}, {
					"data" : "YINGKUI",
					"title":"盈亏(元)",
					"render":function(obj, type, full){
						return formatNumber(full.YINGKUI);
					}
				}, {
					"data" : "DRGSCNT",
					"title":"覆盖病组数",
					"render":function(obj,type,full){
						var OUT_DEPCODE=full.OUT_DEPCODE!=null?full.OUT_DEPCODE:'';//强制转换
						var str="<a href=\"javascript:loadDrgDtl('"+full.OUT_DEPDESC+"','"+full.OUT_DEPCODE+"','"+ param.dateFrom + "','"+ param.dateTo + "')\">"+full.DRGSCNT+"</a>";
						return str;
					}
				}];
			var paramsDataTables = {
					stateSave : true,
					rownumbers : false,
					pageLength : 10,
					searching:true,
					scrollY:'420px',
					order : [ [ 0, "asc" ] ]
				};
			$('#'+tabId+' #dep_quota').dataTableBasic(columns,data,paramsDataTables);
		});
	}
function drgFeeMedinsOutCaseData(depCode,depDesc,dateFrom,dateTo){
	var paramNew= {};
	paramNew.depCode = depCode;// 科室编码
	paramNew.dateFrom = dateFrom;// 开始时间
	paramNew.dateTo = dateTo;// 结束时间
	var desc = depDesc+"|出院病例明细";// 页面描述
	var pageId = depCode + "drgFeeMedinsOutCase";//目标页面的ID
	
	openTab('/pages/drgfeemedins/depOutCase.jsp', desc, pageId, pageId,paramNew);
}
function loadDrgDtl(out_depdesc,out_depcode,dateFrom,dateTo){
		var paramNew= {};
		paramNew.depCode = out_depcode;// 科室编码
		paramNew.dateFrom = dateFrom;// 开始时间
		paramNew.dateTo = dateTo;// 结束时间
		openTab('/pages/drgfeemedins/depQuotaDrgs.jsp',out_depdesc+"|"+"DRGs","dep"+out_depdesc,
				"dep"+out_depdesc,paramNew);
	}